package cn.akwangl.spring.dependency.inject;

import cn.akwangl.spring.dependency.inject.model.UserHolder;
import cn.akwangl.spring.ioc.model.User;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * Java 注解Set方法注入
 *
 * @author youyongkun
 * @date 2021/7/18 10:34 上午
 */
public class AnnotationDependencySetterInjectionDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册配置类
        applicationContext.register(AnnotationDependencySetterInjectionDemo.class);
        // 读取XML BeanDefinition
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
        String location = "classpath:/META-INF/dependency-lookup-context.xml";
        xmlReader.loadBeanDefinitions(location);
        // 刷新上下文
        applicationContext.refresh();

        UserHolder userHolder = applicationContext.getBean(UserHolder.class);
        System.out.println(userHolder);

        // 关闭容器
        applicationContext.close();
    }

    @Bean
    public UserHolder userHolder(User user) {
        UserHolder userHolder = new UserHolder();
        userHolder.setUser(user);
        return userHolder;
    }
}
